﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using System.Text;
using StoragePeriod.Common;
using StoragePeriod.Web.Common;

namespace StoragePeriod.Web.Email
{
    public partial class Email : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "3a392552c961c890";
        }
        #endregion

        #region 函数
        private void BindTreeView()
        {
            this.PersonnelTree.Nodes.Clear();
            List<OperationTeam> OperList = OperationTeam.All().ToList();
            foreach (OperationTeam Oper in OperList.Where(p => p.ParentId == 0))
            {
                TreeNode node = new TreeNode()
                {
                    Value = Oper.id.ToString(),
                    Text = Oper.OTName,
                    ImageUrl = "../images/WebResource.gif",
                    Target = "0",
                    ShowCheckBox = false
                };
                foreach (OperationTeam Team in OperList.Where(p => p.ParentId == Oper.id))
                {
                    TreeNode c_node = new TreeNode()
                    {
                        Value = Team.id.ToString(),
                        Text = Team.OTName,
                        ImageUrl = "../images/person.gif",
                        Target = "0",
                        ShowCheckBox = true
                    };

                    List<SYS_Users> Userlist = SYS_Users.Where("UserType=@0", Team.id).ToList();
                    foreach (SYS_Users user in Userlist)
                    {
                        TreeNode p_node = new TreeNode()
                        {
                            Value = user.id.ToString(),
                            Text = user.UserName,
                            ImageUrl = "../images/WebResource1.gif",
                            Target = "1",
                            ShowCheckBox = true
                        };
                        c_node.ChildNodes.Add(p_node);
                    }
                    node.ChildNodes.Add(c_node);
                }
                this.PersonnelTree.Nodes.Add(node);
            }
            //using (CMSCateringDataContext data = new CMSCateringDataContext())
            //{
            //    foreach (OperationArea area in data.OperationArea)
            //    {
            //        TreeNode node = new TreeNode()
            //        { 
            //            Value=area.id.ToString(),
            //            Text=area.AreaName,
            //            ImageUrl = "../images/WebResource.gif",
            //            Target="0",
            //            ShowCheckBox=false
            //        };
            //        foreach (ClassInfo c in data.ClassInfo.Where(p=>p.AreaID==area.id))
            //        {
            //            TreeNode c_node = new TreeNode()
            //            {
            //                Value=c.id.ToString(),
            //                Text=c.CName,
            //                ImageUrl = "../images/person.gif",
            //                Target="0",
            //                ShowCheckBox=true
            //            };
            //            foreach (Personnel p in data.Personnel.Where(p => p.ClassID == c.id))
            //            {
            //                TreeNode p_node = new TreeNode()
            //                {
            //                    Value=p.id.ToString(),
            //                    Text=p.Name,
            //                    ImageUrl = "../images/WebResource1.gif",
            //                    Target="1",
            //                    ShowCheckBox=true
            //                };
            //                c_node.ChildNodes.Add(p_node);
            //            }
            //            node.ChildNodes.Add(c_node);
            //        }
            //        this.PersonnelTree.Nodes.Add(node);
            //    }
            //}

        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.BindTreeView();
        }

        protected void Btn_Select_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (TreeNode node in this.PersonnelTree.CheckedNodes.AllNodes().Where(p => p.Target == "1"))
            {
                sb.Append(node.Text + ";");
            }
            this.txt_Staff.Text = sb.ToString();
        }

        protected void Btn_submint_Click(object sender, EventArgs e)
        {
            if (this.PersonnelTree.CheckedNodes.AllNodes().Where(p => p.Target == "1").Count() < 1)
            {
                UtilityUI.Alert(this.UpdatePanel1, "请选择收件人!");
                return;
            }

            int count = 0;
            foreach (TreeNode node in this.PersonnelTree.CheckedNodes.AllNodes().Where(p => p.Target == "1"))
            {
                SYS_Users md = SYS_Users.Where("UserCode=@0", User.Identity.Name).First();

                PubEmail pubEmail = new PubEmail();
                pubEmail.Subject = this.txt_title.Text.Trim();
                string str = this.txt_centent.Value.Trim();
                string body = HttpUtility.HtmlDecode(str);
                pubEmail.Body = body;

                SendList send = new SendList();
                send.SendPersonnelID = Convert.ToInt32(md.id);
                send.SendPersonnelName = md.UserName;
                send.PersonnelID = Convert.ToInt32(node.Value);
                send.PersonnelName = node.Text;
                send.Title = this.txt_title.Text.Trim();
                send.SendContent = this.txt_centent.Value.Trim();
                send.SendType = Convert.ToInt32(EnumData.SendType.邮件);
                send.SendDate = DateTime.Now;
                send.IsRead = false;
                send.ReceiverDel = false;
                send.SendDel = false;

                string strEmail = SYS_Users.Where("id=@0", int.Parse(node.Value)).First().EMail;
                if (strEmail != "" && strEmail.IndexOf("@") > 0)
                {
                    count += 1;
                    if (pubEmail.SmtpMailSend(strEmail))
                    {
                        SendList.Insert(send);
                    }
                    else
                    {
                        //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('发送失败!');</script>"); 
                    }
                }

                #region 测试模块，由于人员表中没有可用邮箱
                if (count == 0)
                {
                    PubEmail pubEmail_test = new PubEmail();
                    pubEmail_test.Subject = this.txt_title.Text.Trim();
                    string str_test = this.txt_centent.Value.Trim();
                    string body_test = HttpUtility.HtmlDecode(str_test);
                    pubEmail_test.Body = body_test;

                    if (pubEmail_test.SmtpMailSend(this.txt_search.Text.Trim()))
                    {
                        UtilityUI.Load(this.UpdatePanel1, "alert('发送成功!');window.location.href='SendEmail.aspx';");
                    }
                }
                #endregion
            }
            UtilityUI.Load(this.UpdatePanel1, "alert('发送成功!');window.location.href='SendEmail.aspx';");
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            string strSearch = this.txt_search.Text.Trim();
            if (strSearch == "") return;

            foreach (TreeNode node in this.PersonnelTree.Nodes.AllNodes().Where(p => p.Target == "1" && p.Text.Contains(strSearch)))
            {
                node.Checked = true;
                node.Parent.Expand();
                node.Parent.Parent.Expand();
            }
        }
        #endregion


    }
}